package net.zdsoft.wpchat.support.action.auth;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import net.zdsoft.keel.util.Validators;
import net.zdsoft.wpchat.android.client.BasicAction;
import net.zdsoft.wpchat.support.dto.OperatorDto;
import net.zdsoft.wpchat.support.entity.AuthGroup;
import net.zdsoft.wpchat.support.entity.Operator;
import net.zdsoft.wpchat.support.service.AuthService;
import net.zdsoft.wpchat.support.service.SysOptionService;

/**
 * 用户以及用户权限管理类
 * 
 * @author xuan
 * @version $Revision: 1.0 $, $Date: 2014-9-22 下午8:45:56 $
 */
public class OperatorAction extends BasicAction {
    private static final long serialVersionUID = -6408381007316440764L;

    @Resource
    private AuthService authService;
    @Resource
    private SysOptionService sysOptionService;

    private String operatorId;
    private String userId;
    private String groupId;
    private String regionId;
    private String regionName;
    private String[] groupIds;
    private String loginName;
    private String name;
    private int type;
    private OperatorDto operator = new OperatorDto();
    private List<Operator> operatorList = new ArrayList<Operator>();
    private List<AuthGroup> groupList = new ArrayList<AuthGroup>();

    /**
     * 获取用户列表
     * 
     * @return
     */
    public String listOperator() {
        operatorList = authService.getOperatorsByGroupIds(getSupportMemoryUser().getId(), groupId);
        groupList = authService.getAllGroup(getSupportMemoryUser().getId());
        return SUCCESS;
    }

    /**
     * 冻结用户
     * 
     * @return
     */
    public String freezeOperator() {
        authService.freezeOperator(userId);
        return SUCCESS;
    }

    /**
     * 解冻用户
     * 
     * @return
     */
    public String unfreezeOperator() {
        authService.unfreezeOperator(userId);
        return SUCCESS;
    }

    /**
     * 删除用户
     * 
     * @return
     */
    public String removeOperator() {
        authService.removeOperator(userId);
        return SUCCESS;
    }

    /**
     * 打开添加用户页面
     * 
     * @return
     */
    public String openAddOperator() {
        groupList = authService.getAllGroup(getSupportMemoryUser().getId());
        return SUCCESS;
    }

    /**
     * 添加用户
     * 
     * @return
     */
    public String addOperator() {
        groupList = authService.getAllGroup(getSupportMemoryUser().getId());
        operator.setLoginName(loginName);
        operator.setName(name);
        operator.setRegionName(regionName);
        operator.setRegionRule("000000");
        operator.setGroupIds(groupIds);
        operator.setType(type);
        operator.setCreationTime(new Date());
        if (Validators.isEmpty(loginName)) {
            addActionError("请输入用户帐号");
        }
        else if (!Validators.isAlphanumeric(loginName)) {
            addActionError("用户帐号只能包含字母和数字");
        }

        if (Validators.isEmpty(name)) {
            addActionError("请输入姓名");
        }

        if (Validators.isEmpty(groupIds)) {
            addActionError("请至少选择一个用户组");
        }

        if (hasErrors()) {
            return SUCCESS;
        }

        try {
            authService.addOperator(operator);
            addActionMessage("操作成功");
        }
        catch (Exception e) {
            addActionError("对不起，您添加的帐号已经存在");
        }
        return SUCCESS;
    }

    /**
     * 打开修改用户页面
     * 
     * @return
     */
    public String openModifyOperator() {
        operator = authService.getOperatorById(operatorId);
        groupList = authService.getAllGroup(getSupportMemoryUser().getId());
        return SUCCESS;
    }

    /**
     * 修改用户
     * 
     * @return
     */
    public String modifyOperator() {
        groupList = authService.getAllGroup(getSupportMemoryUser().getId());
        operator.setId(operatorId);
        operator.setLoginName(loginName);
        operator.setName(name);
        operator.setRegionName(regionName);
        operator.setType(type);
        operator.setGroupIds(groupIds);
        if (Validators.isEmpty(loginName)) {
            addActionError("请输入用户帐号");
        }
        else if (!Validators.isAlphanumeric(loginName)) {
            addActionError("用户帐号只能包含字母和数字");
        }

        if (Validators.isEmpty(name)) {
            addActionError("请输入姓名");
        }

        if (Validators.isEmpty(groupIds)) {
            addActionError("请至少选择一个用户组");
        }

        if (hasErrors()) {
            return SUCCESS;
        }

        try {
            authService.modifyOperator(operator);
            addActionMessage("操作成功");
        }
        catch (Exception e) {
            addActionError("对不起，您更改的帐号已经存在");
        }
        return SUCCESS;
    }

    public String getGroupId() {
        return groupId;
    }

    public void setGroupId(String groupId) {
        this.groupId = groupId;
    }

    public List<Operator> getOperatorList() {
        return operatorList;
    }

    public List<AuthGroup> getGroupList() {
        return groupList;
    }

    public String getRegionId() {
        return regionId;
    }

    public void setRegionId(String regionId) {
        this.regionId = regionId;
    }

    public String getRegionName() {
        return regionName;
    }

    public void setRegionName(String regionName) {
        this.regionName = regionName;
    }

    public String getOperatorId() {
        return operatorId;
    }

    public void setOperatorId(String operatorId) {
        this.operatorId = operatorId;
    }

    public OperatorDto getOperator() {
        return operator;
    }

    public String[] getGroupIds() {
        return groupIds;
    }

    public void setGroupIds(String[] groupIds) {
        this.groupIds = groupIds;
    }

    public String getLoginName() {
        return loginName;
    }

    public void setLoginName(String loginName) {
        this.loginName = loginName;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

}
